const {
  NAME_IS_ALREADY_EXISTS,
  NAME_OR_PASSWORD_IS_REQUIRED,
} = require("../config/error");
const userService = require("../service/user-service");
const md5password = require("../utils/md5-password");

const verifyUser = async (ctx, next) => {
  // 验证客户端传递过来的user是否可以保存到数据库中;
  // 验证用户名活着密码是否为空
  const { name, password } = ctx.request.body;
  if (!name || !password) {
    return ctx.app.emit("error", NAME_OR_PASSWORD_IS_REQUIRED, ctx);
  }
  // 判断name是否已在数据库中已经存在
  const users = await userService.findUserByName(name);
  if (users.length) {
    return ctx.app.emit("error", NAME_IS_ALREADY_EXISTS, ctx);
  }
  await next();
};
const handlePassword = async (ctx, next) => {
  // 1.取出密码
  const { password } = ctx.request.body;
  // 2.对密码进行加密
  ctx.request.body.password = md5password(password);
  // 3.执行下一个中间件
  await next();
};

module.exports = { verifyUser, handlePassword };
